﻿using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework;
using System.Xml;
using ProtectThePieWP7.AdditionalClasses;

namespace ProtectThePie
{
    public abstract class Body
    {
        protected bool enabled;
        protected Vector2 position;
        protected Vector2 speed;
        protected float angle;
        protected float radius;
        protected Sprite sprite;

        #region Virtual methods

        public virtual bool Draw(SpriteBatch spriteBatch)
        {
            try
            {
                if (sprite.Draw(spriteBatch, position, 0)) return true;
                else return false;
            }
            catch
            {
                return false;
            }
        }

        public virtual bool Update()
        {
            try
            {
                if (sprite.Update(0, angle) || speed.X != 0 || speed.Y != 0)
                {
                    UpdatePosition();
                    return true;
                }
                else return false;
            }
            catch
            {
                return false;
            }
        }

        public virtual void UpdatePosition()
        {
            Vector2Add.Add(ref position, speed); 
        }

        public virtual bool Contains(Vector2 point)
        {
            if (Vector2.Distance(position, point) < radius + 32)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public virtual Vector2 GetPosition()
        {
            return position;
        }

        public virtual double GetRadius()
        {
            return radius;
        }

        #endregion
        #region Loading/Saving

        public void SaveBody(XmlWriter writer)
        {
            writer.WriteStartElement("Body");

            DataStore.WriteBool(writer, "Enabled", enabled);
            DataStore.WriteVector2(writer, "Position", position);
            DataStore.WriteVector2(writer, "Speed", speed);
            DataStore.WriteFloat(writer, "Angle", angle);
            DataStore.WriteFloat(writer, "Radius", radius);           
            writer.WriteEndElement();

            //writer sprite outside the bodu class
            sprite.Save(writer);
        }

        public void LoadBody(XmlReader reader)
        {
            do
            {
                // Only detect start elements.
                if (reader.IsStartElement())
                {
                    // Get element name and switch on it.
                    switch (reader.Name)
                    {
                        case "Enabled":
                            enabled = reader.ReadElementContentAsBoolean();
                            break;
                        case "Position":
                            reader.Read();
                            position = DataStore.ReadVector2("Position", reader);
                            break;
                        case "Speed":
                            reader.Read();
                            speed = DataStore.ReadVector2("Speed", reader);
                            break;
                        case "Angle":
                            angle = reader.ReadElementContentAsFloat();
                            break;
                        case "Radius":
                            radius = reader.ReadElementContentAsFloat();
                            break;
                        default:
                            reader.Read();
                            break;
                    }
                }
                else
                {
                    if (reader.NodeType == XmlNodeType.EndElement && (reader.Name == "Body")) break;
                    reader.Read();
                }               
            }
            while (!reader.EOF);
        }

        #endregion
    }
}
